//
//  NYMyClassesCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/7.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYMyClassesCell.h"
@interface NYMyClassesCell ()
@property (nonatomic, strong) UIImageView *courseImgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) TTTAttributedLabel *msgLabel;
@property (nonatomic, strong) UIImageView *lineImgView;
@property(nonatomic, strong) TTTAttributedLabel *timeLabel;
@end
@implementation NYMyClassesCell
+ (CGFloat)heightForObject:(id)object identifier:(NSString *)identifier atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
        return __WGHeight(105.0);
}
- (BOOL)shouldUpdateCellWithObject:(id)object
{
        self.classesModel = object;
        return YES;
}
- (void)setClassesModel:(NYMyClassesModel *)classesModel
{
        _classesModel = classesModel;
        [self updateContent];
}

- (void)updateContent
{
        //更新显示数据
        [_courseImgView sd_setImageWithURL:[NSURL URLWithString:NOT_NULL_STRING_DEFAULT_EMPTY(self.classesModel.cover)] placeholderImage:[UIImage selectionHolder]];
        [_titleLabel setText:NOT_NULL_STRING_DEFAULT_EMPTY(self.classesModel.class_title)];
        NSString *msgString = [NSString stringWithFormat:@"共%ld节，已有%ld名学生学习",self.classesModel.course_num.integerValue, self.classesModel.study_num.integerValue];
        [_msgLabel setText:msgString afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
                NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"\\d+" options:(0) error:nil];
                [[regular matchesInString:mutableAttributedString.string options:(0) range:NSMakeRange(0, mutableAttributedString.string.length)] enumerateObjectsUsingBlock:^(NSTextCheckingResult * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)(UIColorHex(0xFF6F40).CGColor) range:obj.range];
                }];
                return mutableAttributedString;
        }];
//        self.timeLabel.text = NOT_NULL_STRING(self.classesModel.time, @"00:00");
}

- (void)setupView {
        [super setupView];
        UIView *backGroundView = [[UIView alloc] init];
        backGroundView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:backGroundView];
        [backGroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).mas_offset(__WGHeight(0));
                make.left.equalTo(self.contentView).mas_offset(__WGHeight(0));
                make.bottom.equalTo(self.contentView).mas_offset(-__WGHeight(0));
                make.right.equalTo(self.contentView).mas_offset(-__WGHeight(0));
        }];
        
        QMUIGhostButton *playBtn = [[QMUIGhostButton alloc] qmui_initWithImage:[UIImage wode_bo_fang] title:nil];
        playBtn.userInteractionEnabled = NO;
        playBtn.ghostColor = [UIColor clearColor];
        [backGroundView addSubview:self.courseImgView];
        [self.courseImgView addSubview:playBtn];
        [self.courseImgView addSubview:self.timeLabel];
        [backGroundView addSubview:self.titleLabel];
        [backGroundView addSubview:self.msgLabel];
        [backGroundView addSubview:self.lineImgView];
        
        [self.courseImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(backGroundView).offset(__WGHeight(14));
                make.left.equalTo(backGroundView).offset(__WGHeight(16));
                make.bottom.equalTo(backGroundView).offset(-__WGHeight(14));
                make.width.equalTo(self.courseImgView.mas_height).multipliedBy(111.0 / 75.0);
        }];
        
        [playBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.courseImgView.mas_bottom).offset(-__WGHeight(5));
                make.left.equalTo(self.courseImgView.mas_left).offset(__WGWidth(5));
        }];
        
        [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(playBtn);
                make.right.equalTo(self.courseImgView.mas_right).offset(-__WGWidth(8));
        }];
        
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.courseImgView.mas_top).offset(__WGHeight(11));
                make.left.equalTo(self.courseImgView.mas_right).offset(__WGHeight(14));
                make.right.lessThanOrEqualTo(backGroundView.mas_right).offset(-__WGHeight(14));
        }];
        
        [self.msgLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.courseImgView.mas_bottom).offset(-__WGHeight(11));
                make.left.equalTo(self.titleLabel);
                make.right.lessThanOrEqualTo(backGroundView).offset(-__WGHeight(14));
        }];
        
        [self.lineImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.courseImgView);
                make.bottom.right.equalTo(backGroundView);
                make.height.mas_equalTo(__WGHeight(1));
        }];
}

#pragma mark - Setter Getter Methods
- (UIImageView *)courseImgView {
        if (!_courseImgView) {
                _courseImgView = [[UIImageView alloc] initWithFrame:CGRectZero];
                _courseImgView.layer.cornerRadius = __WGWidth(3);
                _courseImgView.clipsToBounds = YES;
        }
        return _courseImgView;
}
- (UILabel *)titleLabel {
        if (!_titleLabel) {
                _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
                _titleLabel.font = UIFontMake(15.f);
                _titleLabel.textColor = UIColorMakeWithHex(@"#333333");
        }
        return _titleLabel;
}
- (TTTAttributedLabel *)msgLabel {
        if (!_msgLabel) {
                _msgLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
                _msgLabel.font = WGSystem12Font;
                _msgLabel.textColor = UIColorMakeWithHex(@"#999999");
        }
        return _msgLabel;
}
- (TTTAttributedLabel *)timeLabel {
        if (!_timeLabel) {
                _timeLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
                _timeLabel.font = WGSystem11Font;
                _timeLabel.textColor = UIColorWhite;
        }
        return _timeLabel;
}
- (UIImageView *)lineImgView {
        if (!_lineImgView) {
                _lineImgView = [[UIImageView alloc] initWithFrame:CGRectZero];
                _lineImgView.qmui_borderPosition = QMUIBorderViewPositionBottom;
                _lineImgView.qmui_dashPattern = @[@(2),@(2)];
        }
        return _lineImgView;
}
@end
